/**
 * @file sleep_goal.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_SLEEP!= 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/


/**********************
 *  STATIC VARIABLES
 **********************/
static const lv_point_t sleep_valid_pos[] = {{0, 0}, {0, 1}};
static void sleep_prepare_destory(lv_obj_t * activity_obj);

/**********************
 *      MACROS
 **********************/
#define SLEEP_DEF_ANGLE              5000
#define SLEEP_CIRCLE_START           340
#define SLEEP_CIRCLE_END             3300
#define SLEEP_CIRCLE_R               100
#define SLEEP_CIRCLE_R_OFFSET        1
#define SLEEP_GOAL_MINUTES           480

/**********************
 *   GLOBAL FUNCTIONS
***********************/
static void sleep_goal_add_element(lv_obj_t * tab, app_adptor_sleep_get_time_req * time)
{
    lv_obj_t * tileview = lv_obj_get_parent(lv_obj_get_parent(tab));
    #if USE_LV_WATCH_LAUNCHER2 != 0
    if(lv_watch_get_activity_obj(ACT_ID_SLEEP) == NULL) {
        tileview = launcher_get_tileview_obj();
    }
    #endif
    lv_obj_t * img_color_circle = lv_img_create(tab, NULL);
    LV_ASSERT_MEM(img_color_circle);
    if(img_color_circle == NULL) return;
    lv_img_set_src(img_color_circle, ICON_COLOR_CIRCLE);
    lv_img_set_zoom(img_color_circle,LV_IMG_ZOOM_NONE * lv_obj_get_width(tab) / 240);
    lv_obj_add_style(img_color_circle, LV_IMG_PART_MAIN, &lv_watch_image);
    lv_obj_align(img_color_circle, tab, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t * sleep_goal = lv_img_create(tab, NULL);
    lv_img_set_src(sleep_goal, ICON_STEP_DOT);
    lv_img_set_zoom(sleep_goal,LV_IMG_ZOOM_NONE * lv_obj_get_width(tab) / 240);
    lv_coord_t img_w = lv_obj_get_width(sleep_goal);
    lv_coord_t tab_w = lv_obj_get_width(tab);
    lv_coord_t radius = 0;
    if(tab_w > 240)
    {
        radius = tab_w / 2 - 33 - img_w / 2;
    }
    else
    {
        radius = tab_w / 2 - 18 * tab_w / 240 - img_w / 2;
    }

    lv_coord_t pivot_x = radius * 240 / tab_w;
    lv_img_set_pivot(sleep_goal,
                     -pivot_x,
                     lv_obj_get_height(sleep_goal) / 2);
    lv_obj_align(sleep_goal, tab, LV_ALIGN_IN_RIGHT_MID,
                 radius * 240 / tab_w + img_w - tab_w / 2, 0);

    lv_obj_t * content = lv_watch_cont_create(img_color_circle, LV_LAYOUT_COLUMN_MID, &lv_style_transp_fit, lv_obj_get_width(img_color_circle));
    lv_obj_t * label = lv_label_create(content, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_text_id(label, WATCH_TEXT_ID_SLEEP);
    lv_obj_set_click(label, false);

    lv_bidi_dir_t bidi_dir = lv_obj_get_base_dir(content);

    uint32_t ret = sleep_get_time_req(time);
    label = lv_label_create(content, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font30);
    int32_t sum_min = 0;

    if(ret)
    {
        sum_min = time->longsleep_sum_min + time->shortsleep_sum_min;
    }
    else
    {
        sum_min = 0;
    }

    float delta =  (float)sum_min / SLEEP_GOAL_MINUTES;
    int16_t val = delta * (SLEEP_CIRCLE_END - SLEEP_CIRCLE_START) + SLEEP_CIRCLE_START + 900;
    int16_t val_sub = val % 3600;
    if((val_sub < SLEEP_CIRCLE_START + 900) && (val_sub > 600)) val = 600;
    if(bidi_dir == LV_BIDI_DIR_RTL) {
        val = 5400 - val;
    }

    printf("val is %d\n",val);
    lv_img_set_angle(sleep_goal, val);
    printf("x1 is %d,x2 is %d ,y1 is %d,y2 is %d\n",sleep_goal->coords.x1,sleep_goal->coords.x2,sleep_goal->coords.y1,sleep_goal->coords.y2);

    char strall[50];
    int32_t hour = sum_min/60;
    int32_t min = sum_min%60;
    if(bidi_dir == LV_BIDI_DIR_RTL)
        snprintf(strall, 50, "%s%02d%s%02d", (char *)lv_lang_get_text(WATCH_TEXT_ID_MINUTES),min,
                 (char *)lv_lang_get_text(WATCH_TEXT_ID_HOURS),hour);
    else
        snprintf(strall, 50, "%02d%s%02d%s", hour,(char *)lv_lang_get_text(WATCH_TEXT_ID_HOURS),min,
                 (char *)lv_lang_get_text(WATCH_TEXT_ID_MINUTES));
    lv_label_set_text(label,strall);

    label = lv_label_create(content, NULL);
    if(bidi_dir == LV_BIDI_DIR_RTL) {
        lv_obj_set_base_dir(label, LV_BIDI_DIR_LTR);
        if(LANG_CH == lv_lang_act()) {
            snprintf(strall, 50, "%s%d:%s", (char *)lv_lang_get_text(WATCH_TEXT_ID_ACT_HOURS),
                     SLEEP_GOAL_MINUTES / 60, (char *)lv_lang_get_text(WATCH_TEXT_ID_GOAL));
        } else {
            snprintf(strall, 50, "%s %d:%s", (char *)lv_lang_get_text(WATCH_TEXT_ID_ACT_HOURS),
                     SLEEP_GOAL_MINUTES / 60, (char *)lv_lang_get_text(WATCH_TEXT_ID_GOAL));
        }
    } else {
        if(LANG_CH == lv_lang_act()) {
            snprintf(strall, 50, "%s:%d%s", (char *)lv_lang_get_text(WATCH_TEXT_ID_GOAL),
                     SLEEP_GOAL_MINUTES / 60, (char *)lv_lang_get_text(WATCH_TEXT_ID_ACT_HOURS));
        } else {
            snprintf(strall, 50, "%s:%d %s", (char *)lv_lang_get_text(WATCH_TEXT_ID_GOAL),
                     SLEEP_GOAL_MINUTES / 60, (char *)lv_lang_get_text(WATCH_TEXT_ID_ACT_HOURS));
        }
    }
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_text(label, strall);

    lv_obj_align(content, NULL, LV_ALIGN_CENTER, 0, 15);

    lv_tileview_add_element(tileview, sleep_goal);
    lv_tileview_add_element(tileview, img_color_circle);
    lv_tileview_add_element(tileview, content);
    lv_obj_set_click(sleep_goal, false);
    lv_obj_set_click(img_color_circle, false);
    lv_obj_set_click(content, false);
}

static lv_obj_t * sleep_goal_create(lv_obj_t * tileview, uint16_t id)
{
    lv_obj_t * tab = sleep_tileview_add_tile(tileview, id);
    LV_ASSERT_MEM(tab);
    if(tab == NULL) return NULL;

    lv_sleep_obj_ext_t * ext = sleep_get_ext();
    ext->time = (app_adptor_sleep_get_time_req *)lv_mem_alloc(sizeof(app_adptor_sleep_get_time_req));
    memset(ext->time,0,sizeof(app_adptor_sleep_get_time_req));
    sleep_goal_add_element(tab, ext->time);

    lv_obj_t * label_content = lv_cont_create(tab, NULL);
    lv_obj_add_style(label_content, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_size(label_content, 30,30);
    lv_cont_set_layout(label_content, LV_LAYOUT_OFF);
    lv_obj_t * label_up = lv_label_create(label_content, NULL);
    lv_label_set_text(label_up, LV_SYMBOL_UP);
    lv_obj_align(label_up, label_content, LV_ALIGN_CENTER, 0, 0);
    lv_obj_align(label_content, tab, LV_ALIGN_IN_BOTTOM_MID, 0, -5);
    lv_tileview_add_element(tileview, label_content);
    lv_obj_set_click(label_content, false);

    return tab;
}

static void sleep_create(lv_obj_t * activity_obj)
{
    if(NULL == activity_obj) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_SLEEP;
        activity_ext.create = (lv_watch_activity_create_f_t)sleep_create;
        activity_ext.prepare_destory = sleep_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
    }
     /* watch obj */
    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) {
        return;
    }
    lv_watch_obj_set_anim_mode(obj, LV_WATCH_ANIM_HOR_RIGHT_HIDE);
    lv_sleep_obj_ext_t * ext = lv_obj_allocate_ext_attr(activity_obj, sizeof(lv_sleep_obj_ext_t));
    ext->time = NULL;
    lv_obj_t * img = lv_img_create(obj, NULL);
    lv_img_set_src(img, watch_get_bg());
    lv_obj_set_size(img, LV_HOR_RES, LV_VER_RES);

    lv_obj_t  * tileview = lv_tileview_create(obj, NULL);
    lv_sleep_drop_pull_obj_ext_t * ext_drop = lv_obj_allocate_ext_attr(tileview, sizeof(lv_sleep_drop_pull_obj_ext_t));
    (void)ext_drop;
    LV_ASSERT_MEM(tileview);
    lv_tileview_set_valid_positions(tileview, sleep_valid_pos, 2);
    lv_tileview_set_edge_flash(tileview, true);
    lv_tileview_set_anim_time(tileview, 60);
    lv_page_set_scrollbar_mode(tileview, LV_SCROLLBAR_MODE_OFF);
    lv_obj_add_style(tileview, LV_TILEVIEW_PART_EDGE_FLASH, &lv_watch_cont_opa1);

    lv_obj_t * tab;
    tab = sleep_goal_create(tileview, 0);
    LV_ASSERT_MEM(tab);
    if(tab == NULL) return;

    tab = sleep_info_create(tileview, 1);
    LV_ASSERT_MEM(tab);
    if(tab == NULL) return;

    lv_tileview_set_valid_positions(tileview, sleep_valid_pos, sizeof(sleep_valid_pos) / sizeof(sleep_valid_pos[0]));
    lv_tileview_set_tile_act(tileview, 0, 0, false);
    lv_watch_obj_add_element(tileview);
    lv_watch_obj_add_element(lv_page_get_scrl(tileview));
}


void sleep_create_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e)
        sleep_create(NULL);
}

#if USE_LV_WATCH_LAUNCHER2 != 0
void sleep_create_tab(lv_obj_t * par)
{
    lv_launcher_sleep_tab_ext * ext = lv_obj_allocate_ext_attr(par, sizeof(lv_launcher_sleep_tab_ext));
    ext->time = (app_adptor_sleep_get_time_req *)lv_mem_alloc(sizeof(app_adptor_sleep_get_time_req));
    memset(ext->time,0,sizeof(app_adptor_sleep_get_time_req));
    sleep_goal_add_element(par, ext->time);
}

void sleep_destroy_tab(lv_obj_t * par)
{
    lv_launcher_sleep_tab_ext * ext = lv_obj_get_ext_attr(par);
    if(ext->time) {
        lv_mem_free(ext->time);
        ext->time = NULL;
    }
}
#endif
/**********************
 *   STATIC FUNCTIONS
 **********************/
static void sleep_prepare_destory(lv_obj_t * activity_obj)
{
    lv_sleep_obj_ext_t * ext = sleep_get_ext();
    if(ext->time)
    {
        lv_mem_free(ext->time);
        ext->time = NULL;
    }
    lv_watch_png_cache_all_free();
}

lv_sleep_obj_ext_t * sleep_get_ext(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_SLEEP);
    if(activity_obj == NULL) return NULL;

    if(activity_obj) {
        lv_sleep_obj_ext_t * ext = lv_obj_get_ext_attr(activity_obj);
        return ext;
    }
    return NULL;
}

lv_obj_t * sleep_tileview_add_tile(lv_obj_t * tileview, uint16_t id)
{
    lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
    lv_obj_t * cont = lv_cont_create(tileview, NULL);
    lv_obj_add_style(cont, LV_CONT_PART_MAIN, &lv_style_transp_tight);
    lv_obj_set_size(cont, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_y(cont, LV_HOR_RES * ext->valid_pos[id].y);
    lv_tileview_add_element(tileview, cont);
    lv_obj_set_click(cont, false);
    return cont;
}

uint32_t sleep_get_time_req(app_adptor_sleep_get_time_req *time)
{
    time->longsleep_start_hour = 23;
    time->longsleep_start_min = 50;
    time->longsleep_end_hour = 6;
    time->longsleep_end_min = 49;
    time->longsleep_sum_min = 408;
    time->shortsleep_sum_min = 23;
    return true;
}

#endif /*USE_LV_WATCH_SLEEP*/
